package com.ruoyi.store.controller;

import com.ruoyi.common.core.constant.SecurityConstants;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.InnerAuth;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.store.service.IStockInService;
import com.ruoyi.system.api.domain.store.StockIn;
import com.ruoyi.system.api.dto.StockInDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.List;

/**
 * 入库记录Controller
 *
 * @author lmj
 * @date 2024-10-31
 */
@RestController
@RequestMapping("/stockIn")
@Slf4j
public class StockInController extends BaseController {
    @Autowired
    private IStockInService stockInService;

    /**
     * 查询入库记录列表
     */
    @RequiresPermissions("store:stockIn:list")
    @GetMapping("/list")
    public TableDataInfo list(StockIn stockIn) {
        startPage();
        List<StockIn> list = stockInService.selectStockInList(stockIn);
        return getDataTable(list);
    }

    /**
     * 查询入库记录列表
     */
    @RequiresPermissions("store:stockIn:list")
    @GetMapping("/listPlus")
    public TableDataInfo listPlus(StockInDTO stockInDTO) {
        startPage();
        List<StockIn> list = stockInService.selectStockInListPlus(stockInDTO);
        return getDataTable(list);
    }

    /*用于付款的列表*/
    @RequiresPermissions("store:stockIn:list")
    @GetMapping("/getUsePayList")
    public TableDataInfo getUsePayList(StockInDTO stockInDTO) {
        startPage();
        List<StockIn> list = stockInService.getUsePayList(stockInDTO);
        return getDataTable(list);
    }

    @InnerAuth
    @PostMapping("/getListInStockInCodeList")
    public R<List<StockInDTO>>  getListInStockInCodeList(@RequestBody List<String> codeList) {
        List<StockInDTO> stockInDTOS = stockInService.selectStockInListByCodeList(codeList);
        return R.ok(stockInDTOS);
    }
    @InnerAuth
    @PostMapping("/updateStockInPaidAmount")
    public R<Boolean> updateStockInPaidAmount(@RequestParam("code") String code,
                                              @RequestParam("paidAmount") BigDecimal paidAmount){
        log.info("updateStockInPaidAmount code:{},paidAmount:{}",code,paidAmount);
        return R.ok(stockInService.verified(code,paidAmount)>0);
    }

    @RequiresPermissions("store:stockIn:list")
    @PostMapping("/getStockInList")
    public List<StockIn> getStockList(@RequestBody StockIn stockIn) {
        log.info("stockIn.getStockList:{}", stockIn);
        List<StockIn> list = stockInService.selectStockInList(stockIn);
        return list;
    }

    /**
     * @return com.ruoyi.common.core.web.page.TableDataInfo
     * @description 查询采购退货选择界面采购入库列表
     * @date 2024/11/5 下午2:23
     * @Param stockIn:
     **/
    @RequiresPermissions("store:stockIn:list")
    @GetMapping("/list/buyBack")
    public TableDataInfo listForBuyBack(StockInDTO stockInDTO) {
        log.info("StockInController listForBuyBack stockInDTO:{}", stockInDTO);
        List<StockIn> list = stockInService.selectStockInListForBuyBack(stockInDTO);
        return getDataTable(list);
    }

    /**
     * 导出入库记录列表
     */
    @RequiresPermissions("store:stockIn:export")
    @Log(title = "入库记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, StockIn stockIn) {
        List<StockIn> list = stockInService.selectStockInList(stockIn);
        ExcelUtil<StockIn> util = new ExcelUtil<StockIn>(StockIn.class);
        util.exportExcel(response, list, "入库记录数据");
    }

    /**
     * 获取入库记录详细信息
     */
    @RequiresPermissions("store:stockIn:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id) {
        return success(stockInService.selectStockInById(id));
    }

    /**
     * @return com.ruoyi.common.core.domain.R<com.ruoyi.system.api.domain.warehouse.StockIn>
     * @description 根据id获取入库记录信息
     * @date 2024/11/5 下午4:54
     * @Param id:
     **/
    @GetMapping(value = "/getInfo/returnR/{id}")
    public R<StockIn> getInfoById(@PathVariable Integer id) {
        return R.ok(stockInService.selectStockInById(id));
    }

    /**
     * 新增入库记录
     */
    @RequiresPermissions("store:stockIn:add")
    @Log(title = "入库记录", businessType = BusinessType.INSERT)
    @PostMapping("addStockIn")
    public AjaxResult add(@RequestBody StockIn stockIn) {
        log.info("StockInController add params:{}", stockIn);
        return stockInService.insertStockIn(stockIn);
    }
    @InnerAuth
    @PostMapping("confirmInNo")
    public AjaxResult confirmInNo(@RequestBody StockIn stockIn) {
        log.info("StockInController confirmInNo params:{}", stockIn);
        return stockInService.confirmIn(stockIn);
    }

    @PostMapping("confirmIn")
    public AjaxResult confirmIn(@RequestBody StockIn stockIn) {
        log.info("StockInController confirmIn params:{}", stockIn);
        return stockInService.confirmIn(stockIn);
    }

    /**
     * 修改入库记录
     */
    @RequiresPermissions("store:stockIn:edit")
    @Log(title = "入库记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody StockIn stockIn) {
        log.info("StockInController edit params:{}", stockIn);
        return stockInService.updateStockIn(stockIn);
    }

    @RequiresPermissions("store:stockIn:edit")
    @Log(title = "入库记录", businessType = BusinessType.UPDATE)
    @PostMapping("updStockIn")
    public AjaxResult updStockIn(@RequestBody StockIn stockIn) {
        log.info("StockInController updStockIn params:{}", stockIn);
        return stockInService.updateStockIn(stockIn);
    }

    @InnerAuth
    @PostMapping("getStockInByCode/{code}")
    public R<StockIn> getStockInByCode(@PathVariable("code") String code) {
        return R.ok(stockInService.selectStockInByCode(code));
    }

    @InnerAuth
    @PostMapping("innerUpd")
    public R<Boolean> innerUpd(@RequestBody StockIn stockIn) {
        log.info("innerUpd params:{}", stockIn);
        return R.ok(stockInService.innerUpd(stockIn) > 0);
    }

    /**
     * 删除入库记录
     */
    @RequiresPermissions("store:stockIn:remove")
    @Log(title = "入库记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids) {
        log.info("StockInController remove params:{}", (Object) ids);
        return stockInService.deleteStockInByIds(ids);
    }
}
